﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Text.RegularExpressions;

namespace WelfareLottery.CustomControls
{
    /// <summary>
    /// 该文本控件只能输入一个数字
    /// </summary>
    [Description("一个数字的文本框")]
    public partial class UCANumberOfTextbox : UserControl
    {
        public UCANumberOfTextbox()
        {
            InitializeComponent();
        }

        #region 属性
        /// <summary>
        /// 控件的文本
        /// </summary>
        [Description("文本值"), Browsable(true), Category("自定义属性")]
        public string UCText
        {
            get
            {
                return textBox.Text;
            }
            set
            {
                textBox.Text = value;
            }
        }

        private bool intBool;
        /// <summary>
        /// 只输入正整数
        /// </summary>
        [Description("只输入正整数"), Browsable(true), Category("自定义属性"), DefaultValue(false)]
        public bool IntBool
        {
            get
            {
                return intBool;
            }
            set
            {
                intBool = value;
                if (intBool)
                {
                    textBox.KeyPress += new KeyPressEventHandler(IntBool_KeyPress);
                }
            }
        }

        #endregion


        #region 事件

        private void IntBool_KeyPress(object sender, KeyPressEventArgs e)
        {

            if (((int)e.KeyChar >= 48 && (int)e.KeyChar <= 57 && textBox.Text.Length < 1) || (int)e.KeyChar == 8) //只能输入0-9数字和BackSpace
            {
                e.Handled = false;
            }
            else
            {
                e.Handled = true;
            }



        }
        #endregion
    }
}
